package com.automannn.casClient;

import org.jasig.cas.client.authentication.AuthenticationFilter;
import org.jasig.cas.client.util.HttpServletRequestWrapperFilter;
import org.jasig.cas.client.validation.Cas30ProxyReceivingTicketValidationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author chenkh
 * @time 2022/1/12
 */
@Component
@EnableConfigurationProperties({CasClientProperties.class})
public class CasClientConfiguration {

    @Autowired
    private CasClientProperties clientProperties;

    @Bean
    public FilterRegistrationBean<AuthenticationFilter> casAuthenticationFilterRegistrationBean(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        AuthenticationFilter authenticationFilter = new AuthenticationFilter();
//        authenticationFilter.setCasServerLoginUrl(clientProperties.getCasServerLoginUrl());
//        authenticationFilter.setServerName(clientProperties.getServerName());
        registrationBean.setName("casAuthenticationFilter");
        registrationBean.setFilter(authenticationFilter);
        registrationBean.addInitParameter("casServerLoginUrl",clientProperties.getCasServerLoginUrl());
        registrationBean.addInitParameter("serverName",clientProperties.getServerName());

        return registrationBean;
    }

    @Bean
    /*xxx: ticket验证的过滤器*/
    public FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter> ticketValidationFilterFilterRegistrationBean(){
        FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter> registrationBean = new FilterRegistrationBean<Cas30ProxyReceivingTicketValidationFilter>();
        Cas30ProxyReceivingTicketValidationFilter filter = new Cas30ProxyReceivingTicketValidationFilter();
//        filter.setServerName(clientProperties.getServerName());
//        filter.setRedirectAfterValidation(true);
//        filter.setUseSession(true);

        registrationBean.setName("ticketValidationFilter");

        registrationBean.addInitParameter("casServerUrlPrefix",clientProperties.getCasServerUrlPrefix());
        registrationBean.addInitParameter("serverName",clientProperties.getServerName());
        registrationBean.addInitParameter("redirectAfterValidation","true");
        registrationBean.addInitParameter("useSession","true");
        registrationBean.setFilter(filter);
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean<HttpServletRequestWrapperFilter> httpServletRequestWrapperFilterFilterRegistrationBean(){
        FilterRegistrationBean registrationBean  = new FilterRegistrationBean();
        HttpServletRequestWrapperFilter filter = new HttpServletRequestWrapperFilter();
        registrationBean.setFilter(filter);
        return registrationBean;
    }


}
